package spring.boot.admin.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import spring.boot.admin.mapper.TCommonConfigMapper;
import spring.boot.admin.model.Pages;
import spring.boot.admin.model.TCommonConfig;

@Slf4j
@Service
@DS("two")
public class TCommonConfigService extends ServiceImpl<TCommonConfigMapper, TCommonConfig> {

    public IPage<TCommonConfig> pageList(Integer page, Integer rows) {
        Pages<TCommonConfig> pages = new Pages<>(page, rows);
        QueryWrapper<TCommonConfig> wrapper = new QueryWrapper<>();
        return page(pages, wrapper);
    }

    public String insert(String type, String typeRemark, String keys, String keysRemark,
                         String value, String valueRemark) {
        TCommonConfig tCommonConfig = new TCommonConfig();
        tCommonConfig.setType(type);
        tCommonConfig.setTypeRemark(typeRemark);
        tCommonConfig.setKeys(keys);
        tCommonConfig.setKeysRemark(keysRemark);
        tCommonConfig.setValue(value);
        tCommonConfig.setValuesRemark(valueRemark);
        tCommonConfig.setState(1);
        if (save(tCommonConfig)) {
            return "1";
        } else {
            return "0";
        }
    }

    public String deleteById(Long id) {
        if (removeById(id)) {
            return "1";
        } else {
            return "0";
        }
    }

    public String updateConfigById(Long id, String type, String typeRemark, String keys,
                                   String keysRemark, String value, String valuesRemark) {
        TCommonConfig tCommonConfig = new TCommonConfig();
        tCommonConfig.setId(id);
        tCommonConfig.setType(type);
        tCommonConfig.setTypeRemark(typeRemark);
        tCommonConfig.setKeys(keys);
        tCommonConfig.setKeysRemark(keysRemark);
        tCommonConfig.setValue(value);
        tCommonConfig.setValuesRemark(valuesRemark);
        if (updateById(tCommonConfig)) {
            return "1";
        } else {
            return "0";
        }
    }

    public String updateState(Long id, Integer state) {
        TCommonConfig tCommonConfig = new TCommonConfig();
        tCommonConfig.setId(id);
        tCommonConfig.setState(state);
        if (updateById(tCommonConfig)) {
            return "1";
        } else {
            return "0";
        }
    }
}
